package com.bbm.bbmanage.controller;

import com.bbm.bbmanage.entity.House;
import com.bbm.bbmanage.entity.dto.HouseDTO;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.service.HouseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (House)表控制层
 *
 * @author lionkliu
 * @since 2023-12-06 10:28:22
 */
@RestController
@RequestMapping("house")
public class HouseController {

    @Resource
    private HouseService houseService;

    @GetMapping("/list")
    public ResponseResult list() {
        //LambdaQueryWrapper<House> wrapper=new LambdaQueryWrapper<>();
        ////使用last方法拼接sql语句
        //wrapper.last("limit 50");
        List<House> houseList = houseService.list();
        return ResponseResult.okResult(houseList);
    }

    @GetMapping("/search")
    public ResponseResult searchByTitle(@RequestParam("title") String title,
                                        @RequestParam(value = "price", required = false) Integer price,
                                        @RequestParam(value = "stars", required = false) Integer stars) {
        return houseService.searchByTitle(title, price, stars);
    }

    @GetMapping("/filter")
    public ResponseResult filter(@RequestParam(value = "address", required = false) String address,
                                 @RequestParam(value = "price", required = false) Integer price,
                                 @RequestParam(value = "stars", required = false) Integer stars,
                                 @RequestParam(value = "title") String title) {
        return houseService.filter(address, price, stars, title);
    }


    @GetMapping("/search2")
    public ResponseResult search(@RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("title") String title,
                                 @RequestParam(value = "address", required = false) String address) {
        return houseService.search(pageNum, pageSize, title, address);
    }

    @GetMapping("/page")
    public ResponseResult selectPage(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        return houseService.getHousePage(pageNum, pageSize);
    }

    @GetMapping("/cate/{id}")
    public ResponseResult getByCategoryId(@PathVariable Long id) {
        return houseService.getByCategoryId(id);
    }


    @GetMapping("{id}")
    public ResponseResult getByHouseId(@PathVariable Long id) {
        return houseService.getByHouseId(id);
    }

    @PostMapping
    public ResponseResult insert(@RequestBody House house) {
        return houseService.insertOne(house);
    }


    @PostMapping("/admin")
    public ResponseResult insertHouseDetail(@RequestBody HouseDTO houseDTO) {
        return houseService.insertHouseDetail(houseDTO);
    }


    @PutMapping
    public ResponseResult update(@RequestBody House house) {
        return houseService.updateOne(house);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        return houseService.deleteById(id);
    }

}
